﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.NewArray"/>.
    /// </summary>
    public class NewArrayVM : NewOperatorVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(NewArray),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new NewArrayVM((NewArray)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.NewArray"/>.
        /// </summary>
        public NewArrayVM(NewArray newArray, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(newArray, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.NewArray"/> model.
        /// </summary>
        public NewArray NewArray
        {
            get { return (NewArray)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override void RenderExpression(CodeRenderer renderer, RenderFlags flags)
        {
            // Suppress empty brackets (they'll be included as a part of the type)
            base.RenderExpression(renderer, flags | RenderFlags.NoParensIfEmpty);
        }

        protected override void RenderName(CodeRenderer renderer, RenderFlags flags)
        {
            renderer.RenderText(NewOperator.ParseToken, KEYWORD_BRUSH, this);

            // Omit any brackets (they will be rendered later)
            if (_expressionVM != null)
                _expressionVM.Render(renderer, flags | RenderFlags.SuppressBrackets | RenderFlags.PrefixSpace);
        }

        protected override void RenderStartArguments(CodeRenderer renderer, Brush brush, RenderFlags flags)
        {
            renderer.RenderText(NewArray.ParseTokenStart, brush, this);
        }

        protected override void RenderEndArguments(CodeRenderer renderer, Brush brush, RenderFlags flags)
        {
            if (NewArray.IsEndFirstOnLine)
                renderer.NewLine();
            renderer.RenderText(NewArray.ParseTokenEnd, brush, this);
        }

        protected override void RenderInitializer(CodeRenderer renderer, RenderFlags flags)
        {
            // Render any omitted brackets here (after parameters and before any initializer),
            // skipping the first set, because they will be rendered as the arguments.
            if (_expressionVM != null)
            {
                TypeRefBase typeRefBase = _expressionVM.Expression.SkipPrefixes() as TypeRefBase;
                if (typeRefBase != null)
                    ((TypeRefBaseVM)CreateVM(typeRefBase)).RenderArrayRanks(renderer, flags);
            }

            base.RenderInitializer(renderer, flags);
        }

        #endregion
    }
}
